﻿package {
	
	import away3d.cameras.HoverCamera3D;
	import away3d.core.render.Renderer;
	import away3d.containers.View3D;
	import away3d.containers.Scene3D;
	import away3d.containers.ObjectContainer3D;
	import away3d.core.math.Number3D;
	import away3d.core.utils.Cast;
	import away3d.core.base.Vertex;
	import away3d.cameras.Camera3D;
	import away3d.events.MouseEvent3D;
	import away3d.events.Object3DEvent;
	import away3d.materials.PhongBitmapMaterial;
	import away3d.primitives.Plane;
	import away3d.materials.ColorMaterial;
	import away3d.primitives.Cube;
	import away3d.primitives.Triangle;
	import away3d.lights.DirectionalLight3D;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.system.Security;
	import flash.display.BitmapData;
	import flash.utils.Timer;

	public class Main extends Sprite {
		
		public static const PIXLES_PER_FOOT:Number = 25;
		public static const START_POS:Number = -80;
		
		private var hud:Hud;
		protected var camera:HoverCamera3D;
		protected var view:View3D;
		private var structure:Barn;
		public static const devMode:Boolean = true;
		
		//Controls:
		public var upArrow:MovieClip;
		public var leftArrow:MovieClip;
		public var rightArrow:MovieClip;
		public var downArrow:MovieClip;
		
		public function Main() {
			Security.allowDomain('http://lititzqualitystructures.com/design_center/loader.swf');
			Security.allowDomain('http://lititzqualitystructures.com/');
			addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
		}

		private function onAddedToStage(event:Event):void {
			createScene();
			createStructure();
			
			camera.target = structure;
			camera.z = -1000;
			camera.y = 100;
			
			initHud();
			
			addEventListener(Event.ENTER_FRAME, onRenderTick);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyboardEvent);
			stage.addEventListener(MouseEvent.CLICK, setFocusToStage);
			
			initControls();
			var timer:Timer = new Timer(1500, 1);
			timer.addEventListener(TimerEvent.TIMER, doTempShit);
			timer.start();
		}
		
		private function doTempShit(e:Event):void {
			hud.updateRendering(null);
			structure.visible = true;
		}
		
		private function initHud():void {
			hud = new Hud(this);
		}
		
		private function createScene():void {
			//Create Camera
			camera = new HoverCamera3D();
			
			//Create view3D
			view=new View3D({camera:camera, x:(stage.stageWidth-275)/2 + 275, y:stage.stageHeight/2 - 30, renderer:Renderer.INTERSECTING_OBJECTS});
			addChild(view);
			
			//Create a light scource
			var light:DirectionalLight3D=new DirectionalLight3D({color:0xFFFFFF,ambient:0.9,diffuse:0.9,specular:0.04});
			light.direction = new Number3D(500, -350, 500);
			view.scene.addLight(light);
		}
		
		private function createStructure():void {
			structure = new Barn();
			structure.visible = false;
			structure.yaw( -60);
			view.scene.addChild(structure);
		}
		
		private function handleKeyboardEvent(e:KeyboardEvent):void {
			if (e.keyCode == 37 ||  e.keyCode == 39) structure.rotateOnKeyPress(e.keyCode);
			if (e.keyCode == 38 ||  e.keyCode == 40) moveCamera(e.keyCode);
			if (e.keyCode == 82 && devMode) structure.resize( 500 * Math.random(), 500 * Math.random(), 500 * Math.random() );		//Resize on 'r' (DevFunction)
			if (e.keyCode == 65 && devMode) structure.setRoofPitch(30 + (Math.random() - .2) * 10);		//Change roof angle on 'a' (DevFunction)
			view.render();
		}
		
		public function createTempStructure():void {
			structure.blankOut();
		}
		
		public function setStructureOptions(ridgeVentEnabled:Boolean, wainscotingEnabled:Boolean):void {
			if (ridgeVentEnabled) structure.addRidgeVent();
			else structure.removeRidgeVent();
			
			if (wainscotingEnabled) structure.addWainscoting();
			else structure.removeWainscoting();
		}
		
		public function resizeStructure(inWidth:Number, inDepth:Number, inHeight:Number, inRoofPitch:Number, inOverhang:Number):void {
			if (inWidth < 8 ) throw new Error("Structures must be 8 feet wide or greater.");
			if (inDepth < 8 ) throw new Error("Structures must be 8 feet deep or greater.");
			
			structure.resize(Math.max(inWidth, inDepth) * PIXLES_PER_FOOT, Math.min(inDepth, inWidth) * PIXLES_PER_FOOT, inHeight * PIXLES_PER_FOOT);
			structure.setOverhang(inOverhang);
			structure.setRoofPitch(inRoofPitch);
			
			var midDiagonal:Number = Math.sqrt((inWidth * inWidth) + (inDepth * inDepth));
			var maximumDiagonal:Number = 24;
			camera.z = -1000 * Math.max((midDiagonal / maximumDiagonal), 1);
			
			var colorIndexes:Object = {siding: hud.sidingColor.selectedIndex, trim:hud.trimColor.selectedIndex, roof:hud.roofColor.selectedIndex, wainscoting:hud.wainscotingColor.selectedIndex};
			structure.setStructureTextures(colorIndexes, Boolean(hud.boxRoofCovering.selectedIndex));
			view.render();
		}
		
		private function moveCamera(keyCode:int):void {
			if (keyCode == 38 && view.camera.y < 450) {
				view.camera.tilt(2);
				view.camera.y+=22;
				view.camera.z-=10;
			}else if (keyCode == 40 && view.camera.y > -90 ) {
				view.camera.tilt(-2);
				view.camera.y-=22;
				view.camera.z += 10;
			}
		}
		
		private function initControls():void {
			upArrow.addEventListener(MouseEvent.CLICK, rotateUp);
			leftArrow.addEventListener(MouseEvent.CLICK, rotateLeft);
			rightArrow.addEventListener(MouseEvent.CLICK, rotateRight);
			downArrow.addEventListener(MouseEvent.CLICK, rotateDown);
			
			upArrow.addEventListener(MouseEvent.MOUSE_OVER, rollOverControl);
			leftArrow.addEventListener(MouseEvent.MOUSE_OVER, rollOverControl);
			rightArrow.addEventListener(MouseEvent.MOUSE_OVER, rollOverControl);
			downArrow.addEventListener(MouseEvent.MOUSE_OVER, rollOverControl);
			
			upArrow.addEventListener(MouseEvent.MOUSE_OUT, rollOutControl);
			leftArrow.addEventListener(MouseEvent.MOUSE_OUT, rollOutControl);
			rightArrow.addEventListener(MouseEvent.MOUSE_OUT, rollOutControl);
			downArrow.addEventListener(MouseEvent.MOUSE_OUT, rollOutControl);
		}
		
		private function rotateUp(e:Event) { moveCamera(38); }
		private function rotateLeft(e:Event) { structure.rotateOnKeyPress(37); }
		private function rotateRight(e:Event) { structure.rotateOnKeyPress(39); }
		private function rotateDown(e:Event) { moveCamera(40); }
		
		var controlDropShadow:DropShadowFilter = new DropShadowFilter(2, 90, 0, .75);
		private function rollOverControl(e:MouseEvent):void { e.target.filters = [controlDropShadow]; }
		private function rollOutControl(e:MouseEvent):void { e.target.filters = []; }
		
		public function getBarnTextures():Array {
			return structure.getTextures();
		}
		
		private function setFocusToStage(e:MouseEvent):void {
			if (!e.bubbles) stage.focus = null;
			hud.clearFocus();
		}
		
		public function getWidth():Number {
			return structure.Width/PIXLES_PER_FOOT;
		}
		
		public function getDepth():Number {
			return structure.Depth/PIXLES_PER_FOOT;
		}
		
		protected function onRenderTick(event:Event = null):void {
			view.render();
		}
	}
}